package com.example.xd.default_call

import android.Manifest
import android.app.Activity
import android.app.role.RoleManager
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.telecom.TelecomManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat

class DefaultCallPermissionActivity : AppCompatActivity() {
    private val permissions: Array<String> = arrayOf(
        Manifest.permission.CALL_PHONE,
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.PROCESS_OUTGOING_CALLS,//小米手机需要的权限
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.RECORD_AUDIO
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_default_call_permission)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (DefaultCallUtil.hasPermission(this, permissions)) {
                setDefaultCall()
            } else {
                ActivityCompat.requestPermissions(this, permissions, 1)
            }
        }
    }

    fun setDefaultCall() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val roleManager = getSystemService(RoleManager::class.java)
            val roleIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_DIALER)
            startActivityForResult(roleIntent, 2)
            return
        } else {
            val intent = Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER)
            intent.putExtra(
                TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,
                packageName
            )
            startActivityForResult(intent, 3)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            setDefaultCall()
        } else {
            toast(this, "通话权限获取失败")
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            0 -> {
                if (resultCode == Activity.RESULT_OK) {
                    toast(this, "悬浮窗权限获取成功")
                } else {
                    toast(this, "悬浮窗权限获取失败")
                }
            }
            2 -> {
                if (resultCode == Activity.RESULT_OK) {
                    setFloating()
                } else {
                    toast(this, "默认通话权限获取失败")
                }
            }
            3 -> {
                if (resultCode == Activity.RESULT_OK) {
                    setFloating()
                } else {
                    toast(this, "默认通话权限获取失败")
                }
            }

        }
    }

    fun setFloating() {
        if (!DefaultCallUtil.isFloating(this)) {
            toast(this, "当前无悬浮窗权限，请授权")
            startActivityForResult(
                Intent(
                    Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + packageName)
                ), 0
            )

        }
    }
}